package com.scau402.system1.repository.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scau402.system1.domain.bo.WmsWarehouseBO;
import com.scau402.system1.domain.factory.WmsWarehouseFactory;
import com.scau402.system1.domain.po.WmsWarehouseDO;
import com.scau402.system1.domain.po.WmsWarehouseStoragePO;
import com.scau402.system1.mapper.WmsWarehouseMapper;
import com.scau402.system1.mapper.WmsWarehouseStorageMapper;
import com.scau402.system1.repository.IWmsWarehouseRepository;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author
 * @create 2024/1/3 18:15
 * @description
 */
@Repository
@Slf4j
public class WmsWarehouseRepositoryImpl implements IWmsWarehouseRepository {

    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;

    @Autowired
    private WmsWarehouseStorageMapper wmsWarehouseStorageMapper;

    @Autowired
    private WmsWarehouseFactory wmsWarehouseFactory;

    /**
     * 通过id查询多个仓库的状态
     * @param warehouseIdList 仓库id列表
     * @param tenantId 租户id
     * @return 结果列表
     */
    @Override
    public List<WmsWarehouseBO> findByIds(List<Long> warehouseIdList, Long tenantId) {
        log.info("warehouseIdList：{}", warehouseIdList);
        log.info("tenantId：{}", tenantId);
        List<WmsWarehouseDO> wmsWarehouseDOS = wmsWarehouseMapper.selectBatchIds(warehouseIdList);
        if (Collections.isEmpty(wmsWarehouseDOS)) {
            log.error("仓库为空，warehouseIdList：{}", warehouseIdList);
            return new ArrayList<>(0);
        }
        // 仓库id:仓位列表
        Map<Long, List<WmsWarehouseStoragePO>> wmsWarehouseStorageMap = CollectionUtils.isEmpty(warehouseIdList) ? new HashMap<>(0) : Optional
                .ofNullable(wmsWarehouseStorageMapper.selectList(Wrappers.<WmsWarehouseStoragePO>lambdaQuery()
                        .eq(WmsWarehouseStoragePO::getTenantId, tenantId)
                        .eq(WmsWarehouseStoragePO::getDelFlag, '0')
                        .in(WmsWarehouseStoragePO::getParentWarehouse, warehouseIdList)))
                .orElse(new ArrayList<>(0))
                .stream()
                .collect(Collectors.groupingBy(WmsWarehouseStoragePO::getParentWarehouse));
        // 收集value为空的key列表
        List<Long> emptyKeyList = new ArrayList<>();
        if (Collections.isEmpty(wmsWarehouseStorageMap)) {
            emptyKeyList.addAll(warehouseIdList);
        } else {
            wmsWarehouseStorageMap.forEach((key, value) -> {
                if (Collections.isEmpty(value)) {
                    emptyKeyList.add(key);
                }
            });
        }
        if (emptyKeyList.size() > 0) {
            log.error("以下仓库的仓位为空，warehouseIdList：{}", emptyKeyList);
        }

        List<WmsWarehouseBO> wmsWarehouseBOList = wmsWarehouseFactory.toWmsWarehouseBOList(wmsWarehouseDOS, wmsWarehouseStorageMap);
        log.info("wmsWarehouseBOList：{}", wmsWarehouseBOList);

        return wmsWarehouseBOList;
    }

    /**
     * 根据仓库id查询仓库数据
     * @param warehouseId 仓库id
     * @param tenantId 租户id
     * @return 仓库数据
     */
    @Override
    public WmsWarehouseDO findById(Long warehouseId, Long tenantId) {
        log.info("warehouseId: {}", warehouseId);
        log.info("tenantId: {}", tenantId);
        WmsWarehouseDO wmsWarehouseDO = wmsWarehouseMapper.selectById(warehouseId);
        if(wmsWarehouseDO==null){
            log.error("以下id的仓库不存在: {}",warehouseId);
        }
        return wmsWarehouseDO;
    }

    /**
     * 根据仓位id查询仓位数据
     * @param desStorageId 仓位id
     * @param tenantId 租户id
     * @return 仓位数据
     */
    @Override
    public WmsWarehouseStoragePO findStorageById(Long desStorageId, Long tenantId) {
        log.info("warehouseId: {}", desStorageId);
        log.info("tenantId: {}", tenantId);
        WmsWarehouseStoragePO wmsWarehouseStoragePO = wmsWarehouseStorageMapper.selectById(desStorageId);
        if(wmsWarehouseStoragePO ==null){
            log.error("以下id的仓位不存在: {}",desStorageId);
        }
        return wmsWarehouseStoragePO;
    }

    /**
     * 根据仓库id查询仓库的已使用容量
     * @param warehouseId 仓库id
     * @return 结果
     */
    @Override
    public Long selectUsedCapById(Long warehouseId) {
        return Optional.ofNullable(wmsWarehouseMapper.selectUCById(warehouseId)).orElse(-1L);
    }

    /**
     * 根据仓库id获取仓库名称
     * @param warehouseId 仓库id
     * @return 仓库名称
     */
    @Override
    public String selectNameById(Long warehouseId) {
        return wmsWarehouseMapper.selectNameById(warehouseId);
    }
}
